package org.hismig.cfg;

import org.hismig.utils.StringTool;

/**
 * @declare: //TODO <br />
 *           This class created by Vansion.Hopihit on 2012-9-8 <br />
 *           <a href="mailto:vansionhopihit@gmail.com">Contact Me</a>
 * @version 1.0
 * @author Vansion.Hopihit
 * 
 */
public class Validator {

	public static void validate(Trigger trigger) throws ValidateException {

		if (StringTool.isEmpty(trigger.getClassName())) {
			throw new ValidateException(
					"The Trigger object must to set the className");
		}
	}

	public static void validate(Strategy strategy) throws ValidateException {

		if (StringTool.isEmpty(strategy.getClassName())) {
			throw new ValidateException(
					"The Strategy object must to set the 'className' Property");
		}
	}

	public static void validate(DataBase database) throws ValidateException {

		// 1.database's name
		if (StringTool.isEmpty(database.getName())) {
			throw new ValidateException(
					"The DataBase object must to set the 'name' Property");
		}

		// 2.sourceDBData must to have a DBConnetion
		if (null == database.getSourceDBData().getDbConnection()) {
			throw new ValidateException(
					"The DataBase object must to set the DbConnection cfg");
		}

		// 2.1.DBConnection
		validate(database.getSourceDBData().getDbConnection());

		// 3.distDBData must to have a DBConnection or a DynamicPartition
		if (null == database.getDistDBData().getDbConnection()
				&& null == database.getDistDBData().getDynamicPartition()) {

			throw new ValidateException(
					"The DataBase object must to set a DbConnection or DynamicPartition cfg");
		}

		// 3.1.DBConneciton
		validate(database.getDistDBData().getDbConnection());

		// 3.2.DynamicPartition
		validate(database.getDistDBData().getDynamicPartition());

		// 4 ExtExecutors
		validate("SourcePreExtExecutor", database.getSourcePreExtExecutor());
		validate("DistPreExtExecutor", database.getDistPreExtExecutor());
		validate("DistAfterExtExecutor", database.getDistAfterExtExecutor());
		validate("SourceAfterExtExecutor", database.getSourceAfterExtExecutor());

		// 5 ParamBuilders
		for (ParamBuilder paramBuilder : database.getParamBuilders()) {
			validate(paramBuilder);
		}

		// 6 Tables
		for (Table table : database.getTables()) {
			validate(table);
		}
	}

	public static void validate(Table table) throws ValidateException {
		if (StringTool.isEmpty(table.getName())) {
			throw new ValidateException(
					"The Table object must to set the 'name' Property");
		}

		validate("SourcePreExtExecutor", table.getSourcePreExtExecutor());
		validate("DistPreExtExecutor", table.getDistPreExtExecutor());
		validate("DistAfterExtExecutor", table.getDistAfterExtExecutor());
		validate("SourceAfterExtExecutor", table.getSourceAfterExtExecutor());

		for (ParamBuilder paramBuilder : table.getParamBuilders()) {
			validate(paramBuilder);
		}

		if (null != table.getSelectCondition()) {
			if (StringTool.isEmpty(table.getSelectCondition().getValue())) {
				throw new ValidateException(
						"The SelectCondition object must to set the value");
			}
		}

	}

	public static void validate(ParamBuilder paramBuilder)
			throws ValidateException {

		if (StringTool.isEmpty(paramBuilder.getClassName())) {
			throw new ValidateException(
					"The ParamBuilder object must to set the 'className' Property");
		}
		if (StringTool.isEmpty(paramBuilder.getForName())) {
			throw new ValidateException(
					"The ParamBuilder object must to set the 'for' Property");
		}

	}

	public static void validate(String executorName, ExtExecutor extExecutor)
			throws ValidateException {
		if (null != extExecutor) {

			if (StringTool.isEmpty(extExecutor.getClassName())) {
				throw new ValidateException(String.format(
						"The %s object must to set the 'className' Property",
						executorName));
			}

			if (StringTool.isEmpty(extExecutor.getTo())) {
				throw new ValidateException(String.format(
						"The %s object must to set the 'to' Property",
						executorName));
			}

			if (null == extExecutor.getType()) {
				throw new ValidateException(String.format(
						"The %s object must to set the Type cfg", executorName));
			}

		}
	}

	public static void validate(DynamicPartition dynamicPartition)
			throws ValidateException {

		if (null != dynamicPartition) {
			if (StringTool.isEmpty(dynamicPartition.getClassName())) {
				throw new ValidateException(
						"The DynamicPartition object must to set the 'className' Property");
			}

			if (StringTool.isEmpty(dynamicPartition.getParamByName("driver"))) {
				throw new ValidateException(
						"The DynamicPartition object must to set the 'driver' Property");
			}

			if (StringTool.isEmpty(dynamicPartition.getParamByName("user"))) {
				throw new ValidateException(
						"The DynamicPartition object must to set the 'user' Property");
			}

			if (StringTool.isEmpty(dynamicPartition.getParamByName("password"))) {
				throw new ValidateException(
						"The DynamicPartition object must to set the 'password' Property");
			}

		}

	}

	public static void validate(DBConnection dbConnection)
			throws ValidateException {
		if (null != dbConnection) {
			if (StringTool.isEmpty(dbConnection.getParamByName("driver"))) {
				throw new ValidateException(
						"The DBConnection object must to set the 'driver' Property");
			}

			if (StringTool.isEmpty(dbConnection.getParamByName("url"))) {
				throw new ValidateException(
						"The DBConnection object must to set the 'url' Property");
			}

			if (StringTool.isEmpty(dbConnection.getParamByName("user"))) {
				throw new ValidateException(
						"The DBConnection object must to set the 'user' Property");
			}

			if (StringTool.isEmpty(dbConnection.getParamByName("password"))) {
				throw new ValidateException(
						"The DBConnection object must to set the 'password' Property");
			}

		}
	}

}
